<?php
/*Created by	 : Israel Munoz
Created On		 : 15.08.2011
Modificated On	 : 25.08.2011
About			 : Suppliers and his orders administration pague.
*/

//include the functions.php
require "include/functions.php";
check_session();

//Check the user to be an administrator
check_admin();

//Define Data Variables
$Action='';
$InnerMenu='';
$message="";
$ProductId=0;
$Supplier='';
$SupplierId=0;
$ProductImagePath='';
$ProductSupplierId='';

$OrderProductId=0;
$OrderId=0;

//check if data exist and request it
if(!filter_has_var(INPUT_POST, "Action")){}
else
{
	$Action	= $_REQUEST["Action"];
}

if(!filter_has_var(INPUT_POST, "InnerMenu")){}
else
{
	$InnerMenu	= $_REQUEST["InnerMenu"];
}

if(!filter_has_var(INPUT_POST, "ProductId")){}
else
{
	$ProductId	= $_REQUEST["ProductId"];
}

if(!filter_has_var(INPUT_POST, "Supplier")){}
else
{
	$Supplier	= $_REQUEST["Supplier"];
}
if(!filter_has_var(INPUT_POST, "ProductSupplierId")){}
else
{
	$ProductSupplierId	= $_REQUEST["ProductSupplierId"];
}
if(!filter_has_var(INPUT_POST, "OrderId")){}
else
{
	$OrderId	= $_REQUEST["OrderId"];
}

//Get the Suppliers ID AND Name to Fill the combobox
	$result = Combo_Data("SELECT supplier_id, name FROM supplier ORDER BY supplier_id ASC");
	
	while ($row = mysql_fetch_array($result))
	 {
		$Supplier_Name[] = $row['name'];
		$Supplier_Id[]	 = $row['supplier_id'];
		if ($ProductSupplierId!='')
		{
			if ($ProductSupplierId == $row['name'])
			{
				$ProductSupplierId = $row['supplier_id'];
			}
		}
		
	}

//Get the Products ID AND Name to Fill the combobox
	$result = Combo_Data("SELECT product_id, product_name FROM product ORDER BY product_id ASC");
	
	while ($row = mysql_fetch_array($result))
	 {
		$Product_Name[]  = $row['product_name'];
		$Product_Id[]	 = $row['product_id'];
				
	}


//PERFORM AN ACTION
switch($Action)
{
	case 'SaveProduct':
		//Request Form Values
		
		
		//alert($ProductSupplierId);
		$ProductCategory 	= $_REQUEST["ProductCategory"];
		$ProductName 		= $_REQUEST["ProductName"];
		$ProductDescription = $_REQUEST["ProductDescription"];
		$ProductStock 		= $_REQUEST["ProductStock"];
		$ProductMinStock	= $_REQUEST["ProductMinStock"];
		$ProductImagePath	= $_REQUEST["ProductImagePath"];
		$ProductSell		= $_REQUEST["ProductSell"];
		
		//CALLS SAVE_SUPPLIER FUNCTIONS AND GET THE RESULT MESSAGE
		$message = save_product($ProductSupplierId,$ProductCategory,$ProductName,$ProductDescription,$ProductStock,$ProductMinStock,$ProductImagePath,$ProductId,$ProductSell);
		$ProductId=0;
		break;
		
	case 'DeleteProduct':
		//CALLS DELETE_SUPPLIER FUNCTION AND GET THE RESULT MESSAGE
		$message = delete_product($ProductId);
		$ProductId=0;
		break;

	case 'SaveOrder':
		//Request Form Values
	
		$OrderProductId 	= $_REQUEST["OrderProductId"];
		$OrderQty 			= $_REQUEST["OrderQty"];
		$OrderCost 			= $_REQUEST["OrderCost"];
		$OrderTotal 		= $_REQUEST["OrderTotal"];
		$OrderQtyReceived	= $_REQUEST["OrderQtyReceived"];
		$OrderDateReceived	= $_REQUEST["OrderDateReceived"];
		
		//CALLS SAVE_SUPPLIER FUNCTIONS AND GET THE RESULT MESSAGE
		$message = save_order($OrderProductId,$OrderQty,$OrderCost,$OrderTotal,$OrderQtyReceived,$OrderDateReceived,$OrderId);
		$OrderId=0;
		break;
		
	case 'DeleteOrder':
		//CALLS DELETE_SUPPLIER FUNCTION AND GET THE RESULT MESSAGE
		$message = delete_order($OrderId);
		$OrderId=0;
		break;


		
	default:
	$message="";
}




//DISPLAY MESSAGE
if ($message!="")
{
	alert($message);
}


?>

<html>
<head>
<title>Online Computer Stores - Products Administration</title>
<link rel="stylesheet" href="include/styles.css" type="text/css">
<script src="include/jquery-1.6.2.min.js"></script>
<script src="include/functions.js"></script>
<script>window.onload=function(){
	altRows('alternatecolor');
	altRows('alternatecolor1');
}</script>
</head>


<script>

//Control the Inner Menu
function ShowField(value)
{
		if (value=='ManageProducts') 
	{
			if (document.Data.InnerMenu.value!='')
			{
				 $('#ordersfield').hide('fast');
			}
		
			if (document.Data.InnerMenu.value!='ManageProducts')
			{		
				$('#prodsfield').show('slow');
				document.Data.InnerMenu.value='ManageProducts';
			}
	}

	else if (value=='ManageOrders')  
	{
		
			if (document.Data.InnerMenu.value!='') 
			{
				$('#prodsfield').hide('fast');
			}
			
			if (document.Data.InnerMenu.value!='ManageOrders')
			{
				$('#ordersfield').show('slow');
				document.Data.InnerMenu.value='ManageOrders';
			}
	}
	
	
}

function SaveProduct()
{
	//Check all the input box has data

	if ((document.Data.Supplier.value != '0') && (document.Data.ProductCategory.value != '') && (document.Data.ProductName.value != '') && (document.Data.ProductDescription.value != '') && (document.Data.ProductStock.value != '') && (document.Data.ProductMinStock.value != ''))
	{
				//Submit the action
				document.Data.Action.value="SaveProduct";	
				document.Data.submit();		

	}
	else
	{
		alert ('Please fill all the fields!');
		document.Data.ProductSupplierId.focus();
	}
	
}

function SaveOrder()
{
	//Check all the input box has data

	if ((document.Data.OrderProductId.value != '0') && (document.Data.OrderQty.value != '') && (document.Data.OrderCost.value != '') && (document.Data.OrderTotal.value != ''))
	{
				//Submit the action
				document.Data.Action.value="SaveOrder";	
				document.Data.submit();		

	}
	else
	{
		alert ('Please fill all the fields!');
		document.Data.OrderProductcmb.focus();
	}
	
}

function DeleteProduct(id)
{
	//Check the id
	if (id != 0)
	{
				//Submit the action
				document.Data.ProductId.value=id;	
				document.Data.Action.value="DeleteProduct";	
				document.Data.submit();		

	}
	else
	{
		
		alert ('Please select a record first!');
		document.Data.ProductSupplierId.focus();
	}
	
}

function DeleteOrder(id)
{
	//Check the id
	if (id != 0)
	{
				//Submit the action
				document.Data.OrderId.value=id;	
				document.Data.Action.value="DeleteOrder";	
				document.Data.submit();		

	}
	else
	{
		
		alert ('Please select a record first!');
		document.Data.OrderQty.focus();
	}
	
}


function NewProduct()
{
	document.Data.ProductId.value=0;
	document.Data.ProductSupplierId.selectedIndex = 0;
	document.Data.Supplier.selectedIndex = 0;
	document.Data.ProductCategory.value="";
	document.Data.ProductName.value="";
	document.Data.ProductDescription.value="";
	document.Data.ProductStock.value="";
	document.Data.ProductMinStock.value="";
	document.Data.ProductImageDisplay.src="";
	document.Data.ProductImagePath.value="";
	document.Data.ProductSell.value="";
	
	document.Data.ProductSupplierId.focus();
}

function NewOrder()
{
	document.Data.OrderId.value=0;
	document.Data.OrderProductId.value=0;
	document.Data.OrderProductcmb.selectedIndex = 0;
	document.Data.OrderQty.value="";
	document.Data.OrderCost.value="";
	document.Data.OrderTotal.value="";
	document.Data.OrderQtyReceived.value="";
	document.Data.OrderDateReceived.value="";
	
	document.Data.OrderProductcmb.style.visibility ='visible';

	document.Data.OrderProduct.setAttribute('type','hidden');

	
	document.Data.OrderProductcmb.focus();
}


//This Function assign data by the ID  of a determinated field of a form
function AssignProdId(data, formfield)
{
	data = data.split(';');
	/*$obj->product_id;0
	$obj->sp_name;1
	$obj->category;2
	$obj->product_name;3
	$obj->description;4
	$obj->quantity_in_stock;5
	$obj->minimum_stock_level;6
	$obj->image_path";7
	*/	
	document.Data.Supplier.value 				= data[1];
	//document.Data.ProductSupplierId.selectedIndex = <?=Get_Index($Supplier_Name, isset($_REQUEST["Supplier"]));?>+1;
	document.Data.ProductSupplierId.selectedIndex =data[1];

	document.Data.ProductId.value				= data[0];
	document.Data.ProductCategory.value			= data[2];
	document.Data.ProductName.value				= data[3];
	document.Data.ProductDescription.value 		= data[4];
	document.Data.ProductStock.value			= data[5];
	document.Data.ProductMinStock.value			= data[6];
	document.Data.ProductImageDisplay.src		= data[7];
	document.Data.ProductImagePath.value		= data[7];
	document.getElementById("imagelink").href 	= data[7];
	document.Data.ProductSell.value				= data[8];
	document.Data.ProductName.focus();	
	
	//	eval ("document." + formfield + ".value = id");
}


//This Function assign data by the ID  of a determinated field of a form
function AssignOrderId(data, formfield)
{
	data = data.split(';');
	/*"$obj->stock_order_number; 0
	$obj->product_id 1
	;$obj->product_name 2
	;$obj->order_quantity 3
	;$obj->quantity_received 4
	;$obj->date_last_received 5
	;$obj->unit_cost 6
	;$obj->total_cost"; 7
	*/		

	document.Data.OrderId.value					= data[0];
	
	document.Data.OrderProductcmb.style.visibility ='hidden';
	document.Data.OrderProduct.style.visibility		='visible';
	document.Data.OrderProduct.setAttribute('type','text');
	
	document.Data.OrderProductId.value			= data[1];
	document.Data.OrderProduct.value 			= data[2];
	document.Data.OrderQty.value				= data[3];
	document.Data.OrderQtyReceived.value 		= data[4];
	document.Data.OrderDateReceived.value		= data[5];
	document.Data.OrderCost.value				= data[6];
	document.Data.OrderTotal.value				= data[7];
	
	document.Data.OrderQtyReceived.focus();	
		
}




function SetSupplier (value1)
{
	//ProductSupplierId
	document.Data.Supplier.value = value1;
	
}

function SetOrderProduct (value1)
{
	//ProductSupplierId
	document.Data.OrderProductId.value = value1;
	
}

function SetTotal ()
{
	qty		= document.Data.OrderQty.value;
	value	= document.Data.OrderCost.value
	document.Data.OrderTotal.value= (qty*value);
	document.Data.OrderQtyReceived.focus();
	
}


</script>
<body>
<div align="center">
 <div id="header">
		<div id="topmenu"><ul><?php top_menu(); ?></ul></div> 
            <div id="menu" align="right">
            <form name="form1" method="post" action="search.php">
                <tr>
                <td height="35" width="78"><a>Search Products</a></td>
                <td height="35" width="3">:</td>
                <td height="35" width="294"><input name="mysearch" type="text" id="mysearch">&nbsp;&nbsp;&nbsp;</td>
                </tr>
            </form>
            </div>
		<div id="menubar"> <!-- Menu buttons -->
            <ul>
            <li><a href="index.php" title="Home"><span>Home</span></a></li>
            <li><a href="#" title="Database Administration"><span>Database Maintenance</span></a></li>
            <li><a href="#" title="Stock Control"><span>Stock Levels</span></a></li>
            <li><a href="#" title="Ordering"><span>Ordering</span></a></li>
            </ul>
		</div>
 </div> <!-- Header -->

<div id="page">

<form name="Data" method="post" action="products_admin.php">	
<input type="hidden" name="Action" value="<?=$Action?>" />
<input type="hidden" name="InnerMenu" value="<?=$InnerMenu?>" />

	<h2>Products Administration</h2> 
    	
			<table border=1 width="99%" height="50%">
				<tr>			
					<td width="15%">
                    <h5>Please Select an Option</h5> 
						<input type="button" id="btnProdAdmin" class="newbutton"  value="Manage Products" onClick="Javascript:ShowField('ManageProducts');">
                        <br></br><p>
                        <input type="button" id="btnOrderAdmin" class="newbutton" value="Manage Orders" onClick="Javascript:ShowField('ManageOrders');">
                        <br></br><p>
                        <br></br><p>
                        <br></br><p>
                        <input type="button" id="btnBack" class="newbutton" value="Back" onClick="Javascript:window.location = 'admin_login.php';">
                    </td>
					<td colspan=0>
					<div id='prodsfield'>
                      <fieldset class="man_fieldset" >
                      	<legend>Products</legend> <br>
			            
                        <input type="hidden" name="ProductId" value="<?=$ProductId?>" />
                       <input type="hidden" name="SupplierId" value="<?=$SupplierId?>" />
                                
            			<table border="0" height="100%" width="100%">
							<tr>
                            	<td align="right">Supplier:</td>
                                <td align="left">  <input type="text" name="Supplier" ondrag="return false"  value="" onClick="Javascript:document.Data.ProductSupplierId.focus();" />
                                	<select name="ProductSupplierId" size="1" id="ProductSupplierId" onChange="Javascript:SetSupplier(this.value);">
										<? $option = "<option value='0'> - Select a Supplier - </option>\n";
                                        for ($i = 0; $i < count($Supplier_Name); $i++) {
                                        $option .= "<option ";
                                        $option .= "value=\"$Supplier_Name[$i]\">$Supplier_Name[$i]</option> \n";
                                        }
                                        echo $option;
                                        ?>
                                	</select>
                                </td>
                               	<td align="right">Category:</td>
                                <td  align="left"><input type="textbox" name="ProductCategory" value=""></td>
                            </tr>
                            <tr>
                            	<td align="right">Name:</td>
                                <td  align="left"><input type="textbox" name="ProductName" value=""></td>
                               	<td align="right">Description:</td>
                                <td  align="left"><input type="textbox" name="ProductDescription" value=""></td>

                            </tr>
                              <tr> 
                               	<td align="right">Stock:</td>
                                <td  align="left"><input type="textbox" name="ProductStock" value=""></td>
                            	<td align="right">Min. Stock:</td>
                                <td  align="left"><input type="textbox" name="ProductMinStock" value=""></td>
                            </tr>
							 <tr> 
                               	<td align="right">Sell Price:</td>
                                <td  align="left"><input type="textbox" name="ProductSell" value=""></td>
                            	<td align="right"></td>
                                <td  align="left"></td>
                            </tr>
                            <tr>                        
                               	<td align="right">Image:</td>
                                <td  align="left"><a id="imagelink" href='<?=$ProductImagePath?>' target='new'><img name="ProductImageDisplay" width='50px' height='50px' src="<?=$ProductImagePath?>"/></a></td>
								<input type="hidden" name="ProductImagePath" value="" />                                
                                <td align="right">Upload:</td>
                                <td  align="left"><input type="file" name="ProductImage" value=""></td>


                            </tr>
            				<tr>
                            	<td colspan="2">&nbsp; </td>
                            </tr>
                            <tr>
                               	<td>&nbsp; </td>
                               	<td>&nbsp; </td>
                               	<td>&nbsp; </td>
                            	<td align="right">
                                       	<input  align="right"	type="button" id="btnNewProd"  value="New" onClick="Javascript:NewProduct();"> 
                                       	<input  align="right"	type="button" id="btnSaveProd"  value="Save" onClick="Javascript:SaveProduct();"> 
                                    	<input  align="right" 	type="button" id="btnDelProd"   value="Delete" onClick="Javascript:DeleteProduct(document.Data.ProductId.value);"> 
                                 </td>
                            </tr>
                        </table>            
						</fieldset>
                        
                        <fieldset><legend>Existing Products</legend>
                            <div class="man_list">
                            <table class='altrowstable' id='alternatecolor'>
                                <tr>
                                    <th>Id</th>
                                    <th>Supplier</th>
                                    <th>Category</th>
                                    <th>Name</th>
                                    <th>Description</th>
                                    <th>Stock</th>
                                    <th>Min. Stock</th>
                                    <th>Image</th>
                                    <th>Action</th>                                 
                                </tr> 
                                <?php 
                                    //CALL SHOW_SUPPLIERS PHP FUNCTION AND GET THE DATA
                                    print_r (show_products($Supplier));				
                                ?>
                            </table>
                            </div>
                    </fieldset>
                        
                        
		          </div>
               	  <div id='ordersfield'>
                      <fieldset class="man_fieldset" >
                     
                      	<legend>Orders</legend> <br>
          
                        <input type="hidden" name="OrderId" value="<?=$OrderId?>" />
                                                     
            			<table border="0" height="100%" width="100%">
							<tr>
                            	<td align="right">Product:</td>
                                <td align="left">    <input type="hidden" name="OrderProductId" value="<?=$OrderProductId?>" />
					                                <input type="hidden" name="OrderProduct" value="" />
                                	<select name="OrderProductcmb" size="1" id="OrderProductcmb" onChange="Javascript:SetOrderProduct(this.value);">
										<? $option = "<option value='0'> - Select a Product - </option>\n";
                                        for ($i = 0; $i < count($Product_Name); $i++) {
                                        	$option .= "<option ";
	     									$option .= "value=\"$Product_Id[$i]\">$Product_Name[$i]</option> \n";
										}
                                        echo $option;
                                        ?>
                                	</select>
                                </td>
                               
                             	<td align="right">Qty:</td>
                                <td  align="left"><input type="textbox" name="OrderQty" value=""></td>
                            </tr>
                            <tr> 
                               	<td align="right">Cost:</td>
                                <td  align="left"><input type="textbox" name="OrderCost" value=""></td>
                            	<td align="right">Total:</td>
                                <td  align="left"><input type="textbox" name="OrderTotal" value="" onFocus="Javascript:SetTotal();" ></td>
                            </tr>
							 <tr> 
                               	<td align="right">Qty Received:</td>
                                <td  align="left"><input type="textbox" name="OrderQtyReceived" value=""></td>
                            	<td align="right">Date Received:</td>
                                <td  align="left"><input type="textbox" name="OrderDateReceived" value=""></td>
                            </tr>
                           	<tr>
                            	<td colspan="2">&nbsp; </td>
                            </tr>
                            <tr>
                               	<td>&nbsp; </td>
                               	<td>&nbsp; </td>
                               	<td>&nbsp; </td>
                            	<td align="right">
                                       	<input  align="right"	type="button" id="btnNewProd"  value="New" onClick="Javascript:NewOrder();"> 
                                       	<input  align="right"	type="button" id="btnSaveProd"  value="Save" onClick="Javascript:SaveOrder();"> 
                                    	<input  align="right" 	type="button" id="btnDelProd"   value="Delete" onClick="Javascript:DeleteOrder(document.Data.OrderId.value);"> 
                                 </td>
                            </tr>
                        </table>   
                                
            			</fieldset>
                        
                        <fieldset><legend>Products Order</legend>
                            <div class="man_list">
                                <table class='altrowstable' id='alternatecolor1'>
                                    <tr>
                                        <th>Id</th>
                                        <th>Product</th>
                                        <th>Qty</th>
                                        <th>Cost</th>
                                        <th>Total</th>
                                        <th>Qty Received</th>
                                        <th>Date Received</th>
                                        <th>Action</th>                                 
                                    </tr> 
                                    <?php 
                                        //CALL SHOW_ORDERS PHP FUNCTION AND GET THE DATA
                                        print_r (show_orders());				
                                    ?>
                                </table>
                            </div>
                    	</fieldset>
                        
                	</div>    
					</td>			
				</tr>					
			</table>
	<br>
    <br>
	
</form>
</div> <!-- end of page DIV -->
  <? footer(); ?>
<script>
if (document.Data.InnerMenu.value =="")
{
	$('#prodsfield').hide(1);
	$('#ordersfield').hide(1);
}
else{
	ShowField(document.Data.InnerMenu.value);
}
</script>
</html>